/**
  ******************************************************************************
  * @file    GPIO/GPIO_IOToggle/stm32f4xx_it.c 
  * @author  MCD Application Team
  * @version V1.4.0
  * @date    04-August-2014
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and 
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_it.h"

#include "cJSON.h"
#include "stdio.h"
#include "led.h"
#include "beep.h"
#include "adc.h"
#include "fan.h"
#include "string.h"

//传感器数据
typedef struct SensorData
{
	char light;
	char temperature;
	char humidity;	
}SensorData;

//设备运行状态
typedef struct DeviceStatu
{
	char led1_statu[5];
	char led2_statu[5];
	char led3_statu[5];
	char beep_statu[5];
	char fan_statu[5];
	char fan_level[5];
}DeviceStatu;


char commend_num;// 消息字符串的下标
char commend_buf[128];// 消息字符串缓冲区
uint16_t tim6_period_cnt;	//按键复用计时器计数值

extern char window_num;
extern char commend_buf2[128];//远程消息
extern char commend_flag;//收到的消息的状态，0已读，1未读

extern SensorData sensor_data;//传感器数据
extern DeviceStatu device_statu;//设备状态

extern char mtri_key_value;//矩阵键盘键值
extern char password_input[30];
extern char password[30];
extern char password_input_index;


/** @addtogroup STM32F4xx_StdPeriph_Examples
  * @{
  */

/** @addtogroup GPIO_IOToggle
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/******************************************************************************/
/*            Cortex-M4 Processor Exceptions Handlers                         */
/******************************************************************************/

/**
  * @brief   This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Memory Manage exception.
  * @param  None
  * @retval None
  */
void MemManage_Handler(void)
{
  /* Go to infinite loop when Memory Manage exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Bus Fault exception.
  * @param  None
  * @retval None
  */
void BusFault_Handler(void)
{
  /* Go to infinite loop when Bus Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles Usage Fault exception.
  * @param  None
  * @retval None
  */
void UsageFault_Handler(void)
{
  /* Go to infinite loop when Usage Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
//void SVC_Handler(void)
//{
//}

/**
  * @brief  This function handles Debug Monitor exception.
  * @param  None
  * @retval None
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
//void PendSV_Handler(void)
//{
//}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
//void SysTick_Handler(void)
//{
//}

/******************************************************************************/
/*                 STM32F4xx Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f40xx.s/startup_stm32f427x.s/startup_stm32f429x.s).    */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/
//
///******************************************************************************
//函数名：TIM6_DAC_IRQHandler
//功能：定时器6中断处理函数---主要用作判断按键复用定时器
//输入参数：无
//输出参数：无
//其他说明：
//******************************************************************************/
//void TIM6_DAC_IRQHandler(void)
//{
//	if(TIM_GetITStatus(TIM6, TIM_IT_Update) == SET)
//	{
//		TIM_ClearITPendingBit(TIM6, TIM_IT_Update);
//		tim6_period_cnt++;
//	}
//}
//
///*****************************************************************************************
//函数名：EXTI4_IRQHandler
//功能：外部5-9中断处理函数
//输入参数：无
//输出参数：无
//其他说明：按键1复用功能判断
//
//*****************************************************************************************/
//
//void EXTI4_IRQHandler(void)
//{	
//	
//	static char key1_flag = 0;
//	
//	EXTI_ClearITPendingBit(EXTI_Line4);//清除中断标志
//	if(key1_flag == 0)
//	{
//		key1_flag = 1;
//		TIM_Cmd(TIM6, ENABLE);//基本定时器6开始计时
//	}
//	else if(key1_flag == 1)
//	{
//		key1_flag = 0;
//		TIM_Cmd(TIM6, DISABLE);//基本定时器6停止计时
//		if(tim6_period_cnt <= 8)
//		{
//			tim6_period_cnt = 0;
//		}
//		else if(tim6_period_cnt > 8 && tim6_period_cnt <= 100)//低于1000ms为短按
//		{
//			GPIO_ToggleBits(GPIOE, GPIO_Pin_8);
//			tim6_period_cnt = 0;
//		}
//		else if(tim6_period_cnt > 100)//高于1000ms为长按
//		{
//			switch(window_num)
//			{
//			case 1:
//				window_num = 3;
//				break;
//			case 2:
//				window_num = 1;
//				break;
//			case 3:
//				window_num = 2;
//				break;	
//			}
//			tim6_period_cnt = 0;
//		}
//	}
//	
//}
//
///*****************************************************************************************
//函数名：EXTI9_5_IRQHandler
//功能：外部5-9中断处理函数
//输入参数：无
//输出参数：无
//其他说明：按键2复用功能判断
//
//*****************************************************************************************/
//void EXTI9_5_IRQHandler(void)
//{
//	static char key2_flag = 0;
//	static char key3_flag = 0;
//	static char fan_flag = 0;
//	
//	if(EXTI_GetITStatus(EXTI_Line5) == SET)//判断哪个外部线产生的中断
//	{	
//		EXTI_ClearITPendingBit(EXTI_Line5);//清除中断标志
//		if(key2_flag == 0)
//		{
//			key2_flag = 1;
//			TIM_Cmd(TIM6, ENABLE);//基本定时器6开始计时
//		}
//		else if(key2_flag == 1)
//		{
//			key2_flag = 0;
//			TIM_Cmd(TIM6, DISABLE);//基本定时器6停止计时
//			if(tim6_period_cnt <= 8)
//			{
//				tim6_period_cnt = 0;
//			}
//			else if(tim6_period_cnt > 8 && tim6_period_cnt <= 100)//低于1000ms为短按
//			{
//				GPIO_ToggleBits(GPIOE, GPIO_Pin_9);
//				tim6_period_cnt = 0;
//			}
//			else if(tim6_period_cnt > 100)//高于1000ms为长按
//			{
//				switch(window_num)
//				{
//				case 1:
//					window_num = 2;
//					break;
//				case 2:
//					window_num = 3;
//					break;
//				case 3:
//					window_num = 1;
//					break;	
//				}
//				tim6_period_cnt = 0;
//			}
//		}
//	}
//	else if(EXTI_GetITStatus(EXTI_Line6) == SET)//判断哪个外部线产生的中断
//	{
//		EXTI_ClearITPendingBit(EXTI_Line6);//清除中断标志
//		if(key3_flag == 0)
//		{
//			key3_flag = 1;
//			TIM_Cmd(TIM6, ENABLE);//基本定时器6开始计时
//		}
//		else if(key3_flag == 1)
//		{
//			key3_flag = 0;
//			TIM_Cmd(TIM6, DISABLE);//基本定时器6停止计时
//			if(tim6_period_cnt <= 8)
//			{
//				tim6_period_cnt = 0;
//			}
//			else if(tim6_period_cnt > 8 )//高于80ms消抖
//			{
//				if(fan_flag == 0)
//				{
//					TIM_SetCompare2(TIM12, 1000);
//					fan_flag = 1;
//				}
//				else 
//				{
//					TIM_SetCompare2(TIM12, 0);
//					fan_flag = 0;
//				}
//				
//				
////				fanOpen();
////				GPIO_ToggleBits(GPIOB, GPIO_Pin_15);
//				tim6_period_cnt = 0;
//			}
//		
//		}
//	}
//}
//
//
///******************************************************************************
//函数名：USART2_IRQHandler
//功能：串口2中断处理函数,通过WIFI接收JSON数据实现远程控制
//输入参数：无
//输出参数：无
//其他说明：pd5->tx,	pd6->rx
//******************************************************************************/
//void USART2_IRQHandler(void)
//{
//	if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)//-------------------接收数据中断
//	{
//		USART_ClearITPendingBit(USART2, USART_IT_RXNE);	//清除接收中断标志
//		USART_SendData(USART1, USART_ReceiveData(USART2));//将串口二接收到的数据通过串口一发送出去
//		commend_buf[commend_num] = USART_ReceiveData(USART2);
//		if(commend_buf[0] == '+')
//		{
//			commend_num++;
//		}
//		else
//		{
//			memset(commend_buf,0,sizeof(commend_buf));
//			commend_num = 0;
//		}
//				
//	}
//	if(USART_GetITStatus(USART2, USART_IT_IDLE) == SET)//-------------------总线空闲中断
//	{
//		char* device;
//		char* action;
//		int fan_level;
//		cJSON* cjson;
//		
//		USART_ReceiveData(USART2);		//总线空闲中断通过读取数据来清除中断标志
//		
//		//只获取有效数据，防止AT指令被写入buf缓存区
//		if(commend_buf[0] == '+' && commend_buf[1] == 'I')
//		{	
//			cjson = cJSON_Parse(commend_buf+10);//返回值是一个cJSON结构体所以我们可以通过函数返回结构体的方式选择返回类型
//			device = cJSON_GetObjectItem(cjson,"device")->valuestring;
//			action = cJSON_GetObjectItem(cjson,"action")->valuestring;
//			
//			if(strcmp(device,"LED1") == 0)
//			{
//				if(strcmp(action,"on") == 0)
//				{
//					ledOpen(GPIO_Pin_8);
//				}
//				if(strcmp(action,"off") == 0)
//				{
//					ledClose(GPIO_Pin_8);
//				}
//				sprintf(commend_buf2,"%s:%s",device,action);
//			}
//			else if(strcmp(device,"LED2") == 0)
//			{
//				if(strcmp(action,"on") == 0)
//				{
//					ledOpen(GPIO_Pin_9);
//				}
//				if(strcmp(action,"off") == 0)
//				{
//					ledClose(GPIO_Pin_9);
//				}
//				sprintf(commend_buf2,"%s:%s",device,action);
//			}
//			else if(strcmp(device,"buzzer") == 0)
//			{
//				if(strcmp(action,"on") == 0)
//				{
//					beepOpen();
//				}
//				if(strcmp(action,"off") == 0)
//				{
//					beepClose();
//				}
//				sprintf(commend_buf2,"%s:%s",device,action);
//			}
//			else if(strcmp(device,"fan") == 0)
//			{
//				fan_level = cJSON_GetObjectItem(cjson,"fan_level")->valueint;
//				if(strcmp(action,"on") == 0)
//				{
//					if(fan_level == 0)
//					{
//						TIM_SetCompare2(TIM12, 0);
//						fanOpen();
//					}
//					if(fan_level == 1)
//					{
//						TIM_SetCompare2(TIM12, 400);
//						fanOpen();
//					}
//					if(fan_level == 2)
//					{
//						TIM_SetCompare2(TIM12, 700);
//						fanOpen();
//					}
//					if(fan_level == 3)
//					{
//						TIM_SetCompare2(TIM12, 1000);
//						fanOpen();
//					}
//						
//				}
//				else if(strcmp(action,"off") == 0)
//				{
//					TIM_SetCompare2(TIM12, 0);
//					fanOpen();
//				}
//				sprintf(commend_buf2,"%s:%s,level:%d",device,action,fan_level);
//			}
//			commend_flag = 1;		// 有未读消息
//			cJSON_Delete(cjson);	//释放Json指针堆内存
//		}
//		//清空接收数据缓冲区，为下次接收做准备
//		memset(commend_buf,0,sizeof(commend_buf));
//		commend_num = 0;	
//	}
//}	

/******************************************************************************
函数名：TIM2_IRQHandler
功能：定时器2中断处理函数---主要用作设备自动控制
输入参数：无
输出参数：无
其他说明：
******************************************************************************/
void TIM2_IRQHandler(void)
{

	if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
	{
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
		printf("password error\n");
		if(mtri_key_value == '#')
		{
			if(strcmp(password,password_input) == 0)
			{
				printf("密码正确！\n");
			}
			else
			{
				printf("密码错误！\n");
			}
			memset(password_input,0,sizeof(password_input));
			password_input_index = 0;
		}
//		//led3通过光照传感器实现pwm控制
//		if(sensor_data.light <= 50)
//		{
//			TIM_SetCompare2(TIM1, 1000);
//			ledOpen(GPIO_Pin_10);
//		}
//		else if(sensor_data.light <= 80)
//		{
//			TIM_SetCompare2(TIM1, 300);
//			ledOpen(GPIO_Pin_10);
//		}
//		else if(sensor_data.light <= 100)
//		{
//			TIM_SetCompare2(TIM1, 0);
//			ledOpen(GPIO_Pin_10);
//		}
		
		
		

		

		

		
	}
}

/**
  * @}
  */ 

/**
  * @}
  */ 


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
